<template>
  <div>
    <el-dialog
      :title="obj.isAdd ? '规格添加' : '规格编辑'"
      :visible.sync="obj.isShow"
      width="30%"
    >
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="规格名称">
          <el-input v-model="form.specsname"></el-input>
        </el-form-item>
        <el-form-item
          label="规格属性"
          v-for="(item, index) in specsAttr"
          :key="index"
        >
          <div class="box">
            <el-input v-model="item.value"></el-input>
            <el-button type="primary" v-if="index == 0" @click="changeNum()"
              >新增规格属性</el-button
            >
            <el-button type="danger" v-else @click="delNum(index)"
              >删除</el-button
            >
          </div>
        </el-form-item>
        <el-form-item label="状态">
          <el-switch
            v-model="form.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="cancel()">取 消</el-button>
        <el-button type="primary" @click="add()" v-if="obj.isAdd"
          >添 加</el-button
        >
        <el-button type="primary" @click="edit()" v-else>编 辑</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import { specsAdd, specsInfo,specsEdit } from "../../../request/api.js";
import { sucessAlert, warningAlert } from "../../../utils/alert";
export default {
  props: ["obj"],
  components: {},
  data() {
    return {
      form: {
        specsname: "",
        attrs: "",
        status: 1,
      },
      specsAttr: [
        {
          value: "",
        },
        {
          value: "",
        },
      ],
    };
  },
  computed: {
    ...mapGetters({}),
  },
  methods: {
    ...mapActions({
      specsAscyncList: "specs/asyncList",
      specsAscyncTotal: "specs/asyncTotal",
    }),

    empty() {
      (this.form = {
        specsname: "",
        attrs: "",
        status: 1,
      }),
        (this.specsAttr = [
          {
            value: "",
          },
          {
            value: "",
          },
        ]);
    },
    cancel() {
      (this.obj.isShow = false), this.empty();
    },
    changeNum() {
      this.specsAttr.push({ value: "" });
    },
    delNum(index) {
      this.specsAttr.splice(index, 1);
    },
    add() {
      this.form.attrs = JSON.stringify(
        this.specsAttr.map((item) => item.value)
      );
      specsAdd(this.form).then((res) => {
        if (res.data.code == 200) {
          sucessAlert(res.data.msg);
          this.cancel();
          this.specsAscyncList();
          // 重新请求列表总数
          this.specsAscyncTotal();
        } else {
          // 失败的提示窗
          warningAlert(res.data.msg);
        }
      });
    },
    getOne(id) {
      specsInfo({ id: id }).then((res) => {
        this.form = {
          ...res.data.list[0],
          attrs: JSON.parse(res.data.list[0].attrs),
        };
        this.specsAttr = this.form.attrs.map((item) => ({ value: item }));

        this.form.id = id;
      });
    },
    edit() {
        // console.log(this.form)
      this.form.attrs = JSON.stringify(this.specsAttr.map((item) => item.value)
      );
      specsEdit(this.form).then((res) => {
        if (res.data.code == 200) {
          sucessAlert(res.data.msg);
          this.cancel();
          this.specsAscyncList();
        } else {
          // 失败的提示窗
          warningAlert(res.data.msg);
        }
      });
    },
  },
  mounted() {},
};
</script>
<style scoped>
.box {
  display: flex;
}
.box input {
  flex: 1;
}
</style>